package com.example.demo.utils.listsort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FENGYU
 * @description
 * @date 2023-10-31 11:22
 */
public class ListSort {

    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person(5));
        list.add(new Person(7));
        list.add(new Person(4));
        list.add(new Person(2));
        list.add(new Person(0));
        list.add(new Person(3));
        list.add(new Person(1));
        list.add(new Person(6));
    }


    public static void comp(List<Person> list){
        list.sort(
                new Comparator<Person>() {
                    @Override
                    public int compare(Person o1, Person o2) {
                        if ((o1.getAge() - o2.getAge()) > 0) {
                            return 1;
                        }
                        else if ((o1.getAge() - o2.getAge()) < 0) {
                            return -1;
                        }
                        else {
                            return 0;
                        }
                    }
                }
        );
    }

    public static void lambda(List<Person> list){
        list.sort(((o1,o2) -> {
            //从小到大
            return o1.getAge() - o2.getAge();//此处定义比较规则，o2.age-o1.age即为从大到小
        }));
    }

    public static void coll(List<Person> list){
        Collections.sort(list, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge(); //按数量从大到小排序
            }
        });
    }

    public static List<Person> stream(List<Person> list){
        list = list.stream()
                .sorted(Comparator.comparing(Person::getAge))
                .collect(Collectors.toList());
        return list;
    }

    public static void buubleSort(List<Person> list){
        for (int i = 0; i < list.size(); i++) {
            for(int j = 0;j<list.size() - 1;j++){
                if(list.get(j).getAge() > list.get(j+1).getAge()){
                    Person temp = list.get(j);
                    list.set(j,list.get(j+1));
                    list.set(j+1,temp);
                }
            }
        }

    }
}
